<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_m extends CI_Model {
	function __construct()
    {
        parent::__construct();

    }
	


	function login_check($user,$pass)
	{
	
		$query = $this->db->query("SELECT * FROM d_user WHERE username ='".$user."' and type =1");
		$res =(array)$query->row();
		
		//echo "全站所有密码加密方式".md5(crypt($pass,substr($pass,0,2))); 
		if($res)
		{
			//查询店铺
			$rshop = $this->db->get_where("d_shop","uploadID='".$res["id"]."' or otherUpload like '%".$res['name']."%'")->result_array();

		
			if(count($rshop)>0)
			{
				foreach($rshop as $s)
				{
					$newshop[] = $s['name'];
				}
			}
			$shopname =  $res["shopname"];
			$shopname = unserialize($shopname);
			if($newshop && $shopname)
			{
				$newshopname = array_merge($newshop,$shopname);
			}elseif($newshop)
			{
				$newshopname = $newshop;
			}
			else{
				$newshopname = $shopname;
			}
			$res['shopname'] = serialize($newshopname);
		
			if(md5(crypt($pass,substr($pass,0,2))) == $res["password"])
			{
				
				if($res["password"] =="6dc9709ceaa5acbe5fb6e53b4b9ae540")
				{
					$cookie = array(
					'name'   =>'userid',
					'value'  => $res['id'],
					'expire' => '865000',
					);
					$this->input->set_cookie($cookie); 
					return 203;
				}
				
				//判断是否设置了需要轮换密码
				if($this->config->item('changwp')>0 && $res["changetime"] !='0000-00-00 00:00:00')
				{
					//判断需要设置轮换密码
					$needtime=date("Y-m-d", strtotime("+{$this->config->item('changwp')} months", strtotime($res["changetime"])));
					if(date("Y-m-d") == $needtime)
					{
						return 204;
					}
				}
				$cookie = array(
					'name'   =>'userid',
					'value'  => $res['id'],
					'expire' => '865000',
				);
				$this->input->set_cookie($cookie); 

				$cookie = array(
					'name'   =>'username',
					'value'  => $res['name'],
					'expire' => '865000',
				);
				$this->input->set_cookie($cookie); 

				$cookie = array(
					'name'   =>'useraccount',
					'value'  => $res['username'],
					'expire' => '865000',
				);
				$this->input->set_cookie($cookie); 

				$cookie = array(
					'name'   =>'classid',
					'value'  => $res['classid'],
					'expire' => '865000',
				);
				$this->input->set_cookie($cookie); 
				$cookie = array(
					'name'   =>'department',
					'value'  => $res['department'],
					'expire' => '865000',
				);
				$this->input->set_cookie($cookie); 

				/***********************************
				*** 如果用户本身没有权限。则继承用户组的权限
				******************************/
				/*
				if($res['power']=="")
				{

					$group = $this->db->get_where('d_group',"id='".$res["department"]."'")->result_array();
					if($group[0]["rules"])
					{
						$power = $group[0]["rules"];
					}else{
						$power = "";
					}
				}else{
					$power = $res['power'];
				}*/
				/***********************************
				*** 合并用户组的权限
				******************************/
				$group = $this->db->get_where('d_group',"id='".$res["department"]."'")->result_array();
				if($res["power"] == 'all'){
					$newpower = 'all';
				}elseif($res["power"] && $group[0]["rules"])
				{
					$newpower = $res["power"].",".$group[0]["rules"];
				}elseif($res["power"] &&  $group[0]["rules"]=='' )
				{
					$newpower = $res["power"];
				}elseif($res["power"] =='' &&  $group[0]["rules"])
				{
					$newpower = $group[0]["rules"];
				}else{
					$newpower = "";
				}
				if($newpower)
				{
					//去空，去重量
					$powerstr = array_unique(array_filter(explode(",",$newpower)));
					$power = implode(",",$powerstr);
				}else{
					$power="";
				}
			

				$cookie = array(
					'name'   =>'power',
					'value'  => $power,
					'expire' => '865000',
				);
				$this->input->set_cookie($cookie); 
			

				$cookie = array(
					'name'   =>'seawarehouse',
					'value'  => $res['seawarehouse'],
					'expire' => '865000',
				);
				$this->input->set_cookie($cookie); 
				$cookie = array(
					'name'   =>'shopname',
					'value'  => $res['shopname'],
					'expire' => '865000',
				);
				$this->input->set_cookie($cookie); 
				
				//读取外部员工权限
				
				if($res['classid']==14)
				{
					$exter = $this->db->get_where("d_user_external","external_user like '%".$res['name']."%'")->result_array();
					$exter = $exter[0];
					if($exter)
					{
						if($exter['is_stop'] == 1)
						{
							die('Error！Login failed！');
						}
						$cookie = array(
							'name'   =>'ext_userid',
							'value'  => $exter['external_userID'],
							'expire' => '865000',
						);
						$this->input->set_cookie($cookie); 
						$cookie = array(
							'name'   =>'ext_userid_open_goods',
							'value'  => $exter['is_opengoods']?$exter['is_opengoods']:0,
							'expire' => '865000',
						);
						$this->input->set_cookie($cookie); 
						$cookie = array(
							'name'   =>'external_id',
							'value'  => $exter['id'],
							'expire' => '865000',
						);
						$this->input->set_cookie($cookie); 
					}else{
						setcookie("salt","exsoftorder");
						delete_cookie("userid");
						delete_cookie("username");
						delete_cookie("user_login");
						return 213;
						//die('没有设置外部组！不允许登录!');
					}
				}else{
						$exter = $this->db->get_one("select GROUP_CONCAT(external_userID) as notex_userid from d_user_external where 1 and is_stop = 0");
						$this->input->set_cookie($cookie); 
						$cookie = array(
							'name'   =>'notext_userid',
							'value'  => $exter['notex_userid'],
							'expire' => '865000',
						);
						$this->input->set_cookie($cookie); 
				
				}
		
				
				
				
		
				return 1;
			}else{
				
				return 202;
			}

			
		  }else{
			 
			return 201;
		}
		
	}
	


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */